package spring.cloud.simple.ui.service;

import java.util.List;
import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import spring.cloud.simple.ui.model.User;

@Service
public class UserService {
    @Autowired
    private RestTemplate restTemplate;

    private final String SERVICE_NAME = "spring-cloud-simple-service";

    @HystrixCommand(fallbackMethod = "fallbackFindAll")
    public List<User> findAll() {
        return restTemplate.getForObject("http://" + SERVICE_NAME + "/user" , List.class);
    }

    private List<User> fallbackFindAll() {
        System.out.println("fallbackFindAll!");
        List<User> users = new ArrayList<>();
        User user = new User();
        user.setId("1");
        user.setUsername("fallbackFindAll");
        users.add(user);
        return users;
    }
}
